package com.company.project.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import com.company.project.common.utils.DataResult;
import com.company.project.common.utils.EnumAreaType;
import com.company.project.entity.CommonUserEntity;
import com.company.project.entity.SysUser;
import com.company.project.service.CommonUserService;
import com.company.project.vo.resp.CommonUserWithAreaDTO;

/**
 * @author ls
 * @email *****@mail.com
 * @date 2021-02-02 20:27:58
 */
@Controller
@RequestMapping("/")
@Api(tags = "创始人联系人模块")
public class CommonUserController extends BaseController{
    @Autowired
    private CommonUserService commonUserService;

    /**
    * 跳转到页面
    */
    @GetMapping("/index/commonUser")
    public String commonUser() {
        return "commonuser/list";
    }
    
    /**
     * 跳转到页面
     */
     @GetMapping("/index/commonUserLastDay")
     public String commonUserLastDay() {
         return "commonuser/listLastDay";
     }

    @ApiOperation(value = "新增")
    @PostMapping("commonUser/add")
    @RequiresPermissions("commonUser:add")
    @ResponseBody
    public DataResult add(@RequestBody CommonUserEntity commonUser){
        commonUserService.save(commonUser);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("commonUser/delete")
    @RequiresPermissions("commonUser:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        commonUserService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("commonUser/update")
    @RequiresPermissions("commonUser:update")
    @ResponseBody
    public DataResult update(@RequestBody CommonUserEntity commonUser){
        commonUserService.updateById(commonUser);
        return DataResult.success();
    }

    @SuppressWarnings({"rawtypes" })
	@ApiOperation(value = "查询分页数据")
    @PostMapping("commonUser/listByPage")
    @RequiresPermissions("commonUser:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody CommonUserEntity commonUser){
    	
    	SysUser currentUser = getCurrentUser();		
		String username = currentUser.getUsername();		
		String area = EnumAreaType.getIndexByCode(username);
		
        Page page = new Page(commonUser.getPage(), commonUser.getLimit());
        IPage<CommonUserWithAreaDTO> iPage = commonUserService.getList(page, commonUser.getStartTime(), commonUser.getEndTime(), area);
        return DataResult.success(iPage);
    }
    
    @SuppressWarnings({"rawtypes" })
	@ApiOperation(value = "查询分页数据昨日")
    @PostMapping("commonUser/listByPageLastDay")
    @RequiresPermissions("commonUser:listLastDay")
    @ResponseBody
    public DataResult findListByPageLastDay(@RequestBody CommonUserEntity commonUser){
    	SysUser currentUser = getCurrentUser();		
		String username = currentUser.getUsername();		
		String area = EnumAreaType.getIndexByCode(username);
    	
    	Page page = new Page(commonUser.getPage(), commonUser.getLimit());        
        Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时间         
        calendar.add(Calendar.DATE, -1);    //得到前一天
        String  yestedayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        String start = yestedayDate+" 00:00:00";
        String end = yestedayDate+" 23:59:59";		
        IPage<CommonUserWithAreaDTO> iPage = commonUserService.getList(page, start, end, area);
        return DataResult.success(iPage);
    }

}
